package ar.com.photo_admin.service

import ar.com.photo_admin.domain.*

class ShoppingCartService extends BaseService {

	def eventPhotoService
	
	/**
	 * Finds the shopping cart for the logged in user.
	 */
	def findCartForLoggedInUser() {
		return findCartForUser(getLoggedUser())
	}
	
	/**
	 * Finds the shopping cart for the given user.
	 */
	def findCartForUser(user) {
		ShoppingCart cart = ShoppingCart.findByUser(user)
		
		if (!cart) {
			log.info "Creating cart for user: ${user.username}"
			cart = new ShoppingCart(user: user)
			cart.save(flush:true)
		}
	
		return cart
	}
	
	/**
	 * Adds the given photo to the shopping cart of the logged in user.
	 */
    def addPhotoToCart(photoId) {	
		ShoppingCart cart = findCartForLoggedInUser()
		addPhotoToCart(photoId, cart)
    }
	
	/**
	 * Adds the photo to the given cart if the user of the cart has permission.
	 */
	def addPhotoToCart(photoId, ShoppingCart cart) {
		EventPhoto selectedPhoto = EventPhoto.get(photoId)

		// Checks whether the photo is already in the shopping cart or not.
		ShoppingCartItem item = ShoppingCartItem.findByPhotoAndShoppingCart(selectedPhoto, cart)
		
		if (!item && cart.user.isAllowed(selectedPhoto)) {
			item = new ShoppingCartItem(photo : selectedPhoto)				
			cart.addToItems(item).save(flush:true)
		}
	}
	
	/**
	 * Removes the given photo from the shopping cart of the logged in user.
	 */
	def removePhotoFromShoppingCart(photoId) {
		ShoppingCart cart = findCartForLoggedInUser()
		EventPhoto photo = EventPhoto.get(photoId)
		
		cart.removePhoto(photo)
	}
	
	/**
	 * Adds all the photos from the given album if the logged in user has permission.
	 */
	def addPhotosFromAlbum(albumId) {
		ShoppingCart cart = findCartForLoggedInUser()
		
		EventAlbum album = EventAlbum.get(albumId)
		
		if (cart.user.isAllowed(album))
			album.getAlbumPhotos().each {
				addPhotoToCart(it.id, cart)
			}
	}
	
	/**
	 * Gets the shopping cart's photos in JSON format.
	 */
	def getAllPhotosInShoppingCartAsJSON(){
		ShoppingCart cart = findCartForLoggedInUser()					
		def photos = cart.getSelectedPhotos()
		
		return eventPhotoService.convertPhotosToJSON(photos)
	}
	
	/**
	 * Clears the shopping cart of the logged in user.
	 */
	def emptyShoppingCart() {
		ShoppingCart cart = findCartForLoggedInUser()	
		
		def l = []
		l += cart.items
		
		l.each { item ->
		    cart.removeFromItems(item)
		}		
	}
	
	/**
	 * Removes the photos from the shopping cart of the logged in user,
	 * that belong to an event from the given photographer.
	 */
	def emptyShoppingCart(photographerId) {
		Photographer photographer = Photographer.get(photographerId)
		emptyShoppingCartForUser(getLoggedUser(), photographer)
	}
	
	/**
	 * Removes the photos from the shopping cart of the given user,
	 * that belong to an event from the given photographer.
	 */
	def emptyShoppingCartForUser(User user, Photographer photographer) {
		ShoppingCart cart = findCartForUser(user)
				
		def l = []
		l += cart.items
		
		l.each { item ->
			if ( item.photo.event.photographer == photographer)
				cart.removeFromItems(item)
		}		
	}
	
	/**
	 * Checks whether the given photo is in the shopping cart or not.
	 */
	def isPhotoInLoggedUserCart(EventPhoto photo) {
		User user = getLoggedUser()
		def inCart = false
		
		if (user) {
			ShoppingCart cart = findCartForUser(user)
			inCart = ShoppingCartItem.findByPhotoAndShoppingCart(photo, cart) != null
		}
		
		return inCart	
	}
	
	/**
	 * Gets a map with all the photos by photographer in the cart.
	 */
	def getShoppingCartPhotographerPhotosMap() {
		ShoppingCart cart = findCartForLoggedInUser()
		
		def map = [:]
		
		def photographers = cart.getPhotographers()
		
		photographers.each { Photographer photographer ->
			
			def photos = cart.getPhotosByPhotographer(photographer)
			map[photographer] = photos
		}
		
		return map
	}
	
	/**
	 * Gets the photographer owner of most photos in the cart with the photos in JSON format.
	 */
	def getPhotographerOwnerOfMostPhotosInCartAndItsPhotosInJSON(){
		def map = getShoppingCartPhotographerPhotosMap()
		
		def max = 0
		def photographer = null
		
		map.each { k, v ->
			if (v.size() > max) {				
				photographer = k
				max = v.size()
			}
		}		
		
		def photos = map[photographer]
		
		return [photographer, eventPhotoService.convertPhotosToJSON(photos)]		
	}
	
	/**
	 * Gets the photographers that has at least one photo in the shopping cart of the logged in user. 
	 */
	def getPhotographersOwnerOfPhotosInCart(){
		return getShoppingCartPhotographerPhotosMap().keySet()
	}
	
	/**
	 * Gets the photos of the given photographer that are in the logged in user shopping cart in JSON format.
	 */
	def getPhotographerAndPhotographerPhotosInCartInJSON(photographerId) {
		Map<Photographer, List<EventPhoto>> map = getShoppingCartPhotographerPhotosMap()
			
		def photographer = null
		def photos = null
		
		map.each { k, v ->	
			if (k.id == photographerId) {				
				photographer = k
				photos = v				
			}												
		}
		
		return [photographer,eventPhotoService.convertPhotosToJSON(photos)]
	}
	
	/**
	 * Determines whether the shopping cart has elements.
	 */
	def hasPhotosInShoppingCart(User user) {
		ShoppingCart cart = findCartForUser(user)
		return !cart.items?.isEmpty()
	}
}
